use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::iter::IntoIterator;
use crate::models::response_object::{ResponseObject, ResponseType};

#[derive(Debug, Serialize, Deserialize)]
pub struct ListResponse<T> {
    #[serde(flatten)]
    base: ResponseObject<Vec<T>>,
}

impl<T> ListResponse<T> {
    pub fn new() -> Self {
        ListResponse {
            base: ResponseObject {
                response_type: ResponseType::List, // 默认类型为 Object，可以根据需要修改
                code: 200,
                msg: "success".to_string(),
                data: Some(Vec::new()),
            },
        }
    }

    pub fn with_list(list: Vec<T>) -> Self {
        ListResponse {
            base: ResponseObject {
                response_type: ResponseType::List,
                code: 200,
                msg: "success".to_string(),
                data: Some(list),
            },
        }
    }

    pub fn stream(&self) -> impl Iterator<Item = &T> {
        self.base.data.as_deref().unwrap_or(&[]).iter()
    }

    pub fn for_each<F>(&self, action: F)
    where
        F: FnMut(&T),
    {
        self.base.data.as_deref().unwrap_or(&[]).iter().for_each(action);
    }

    pub fn iterator(&self) -> impl Iterator<Item = &T> {
        self.base.data.as_deref().unwrap_or(&[]).iter()
    }




}

impl<T: Debug + PartialEq> std::fmt::Display for ListResponse<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ListResponse(base={})", self.base)
    }
}


